package com.lg.erp.controller;


import com.lg.erp.entity.JsonResult;
import com.lg.erp.util.FtpUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Controller
public class UploadController {


    public static final String HOST="192.168.31.100";
    public static final Integer PORT=21;
    public static final String BASEPATH="/home/ftpuser/images";
    public static final String USERNAME="ftpuser";
    public static final String PASSWORD="j1738934";

    @ResponseBody
    @PostMapping("/upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename=file.getOriginalFilename();
        filename= UUID.randomUUID().toString().replace("-","")+
            filename.substring(filename.lastIndexOf("."));
        FtpUtil.upload(HOST,PORT,
                USERNAME,
                PASSWORD,
                BASEPATH,
                filename,
                file.getInputStream());
        return new JsonResult(1,"ok",filename);
    }
}
